package vh.android.multiconnector.quiz.ui.adapters;

import java.util.ArrayList;

import vh.android.multiconnector.quiz.R;
import vh.android.multiconnector.quiz.models.GamePlayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PlayerAdapter extends BaseAdapter {

	private ArrayList<GamePlayer> mPlayers = new ArrayList<GamePlayer>();
	private Context mContext;

	public PlayerAdapter(Context pContext) {
		mContext = pContext;
	}

	@Override
	public int getCount() {
		return mPlayers.size();
	}

	@Override
	public GamePlayer getItem(int pIndex) {
		return mPlayers.get(pIndex);
	}

	@Override
	public long getItemId(int pIndex) {
		return pIndex;
	}

	@Override
	public View getView(int pIndex, View pView, ViewGroup pViewGroup) {
		GamePlayer player = getItem(pIndex);

		if (pView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			pView = inflater.inflate(R.layout.row_player, null);
		}

		TextView tvPlayerName = (TextView) pView
				.findViewById(R.id.tvPlayerName);
		tvPlayerName.setText(player.getName());
		
		return pView;
	}

	public void addAll(GamePlayer[] gamePlayers) {
		for (GamePlayer a : gamePlayers)
			mPlayers.add(a);
		notifyDataSetChanged();
	}
}
